package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         1679,
		Discription: "数组中和为k的数对的数量",
		Level:       2,
		Labels: map[string]int{
			"哈希表":  1,
		},
	})
}

func MaxOperations(nums []int, k int) int {
	valMap := make(map[int]int, 0)
	for _, val := range nums {
		valMap[val]++
	}

	res := 0
	for val:= range valMap {
		//考虑值为k的一半的情况
		if val == k-val {
			for valMap[val] >= 2 {
				res++
				valMap[val] -= 2
			}
		} else {
			for valMap[val] > 0 && valMap[k-val] > 0 {
				res++
				valMap[val]--
				valMap[k-val]--
			}
		}
	}

	return res
}